package cn.yuanqiao.manage.service.impl;/**
 * @author 赵常飞
 * @date 2024/1/24 16:56
 */


import cn.yuanqiao.common.core.domain.TreeSelect;
import cn.yuanqiao.common.core.domain.model.LoginUser;
import cn.yuanqiao.common.utils.DateUtils;
import cn.yuanqiao.common.utils.SecurityUtils;
import cn.yuanqiao.common.utils.SnowflakeIdGenerator;
import cn.yuanqiao.manage.domain.YqdaGdfwAndBgqx;
import cn.yuanqiao.manage.domain.YqdaGdfwAndBgqxRule;

import cn.yuanqiao.manage.mapper.GdfwAndBgqxMapper;
import cn.yuanqiao.manage.service.IGdfwAndBgqxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *@Author: 赵常飞
 *@Date: 2024/1/24 16:56
 *@Description:
 *@Note:
 */
@Service
public class GdfwAndBgqxServiceImpl implements IGdfwAndBgqxService {
    @Autowired
    private GdfwAndBgqxMapper gdfwAndBgqxMapper;

    @Override
    public List<TreeSelect> selectTreeList(Long categoryId) {
        YqdaGdfwAndBgqx yqTrees=new YqdaGdfwAndBgqx();
        List<YqdaGdfwAndBgqx> list = gdfwAndBgqxMapper.getTreeListfirst(categoryId);
        TreeSelect rootTree = new TreeSelect();
        rootTree.setId(0L);
        rootTree.setLabel("根节点");
        List<TreeSelect> chilTrees = new ArrayList<>();
        for(YqdaGdfwAndBgqx yqdaTrees:list){
            TreeSelect chilTree = new TreeSelect();
            chilTree.setId(yqdaTrees.getId());
            chilTree.setLabel(yqdaTrees.getName());
            chilTree.setBgqx(yqdaTrees.getBgqx());
            chilTree.setOrder_no(yqdaTrees.getOrderNo());
            chilTree.setChildren(getchildrens(yqdaTrees.getId(),categoryId));
            chilTrees.add(chilTree);
        }
        rootTree.setChildren(chilTrees);
        List<TreeSelect> treeSelects = new ArrayList<>();
        treeSelects.add(rootTree);
        return treeSelects;
    }

    @Override
    public int addzd(YqdaGdfwAndBgqx gdfwAndBgqx) {
        LoginUser user = SecurityUtils.getLoginUser();
        if(user == null){
            user = new LoginUser();
            user.setUserId(1L);
        }
        gdfwAndBgqx.setCREATOR(user.getUsername());
        gdfwAndBgqx.setCreatorId(user.getUserId());
        gdfwAndBgqx.setCreateTime(DateUtils.getNowDate());
        gdfwAndBgqx.setIsDel(0);
        Integer i=gdfwAndBgqxMapper.addzd(gdfwAndBgqx);
        return 1;
    }

    @Override
    public int editzd(YqdaGdfwAndBgqx gdfwAndBgqx) {
        LoginUser user = SecurityUtils.getLoginUser();
        if(user == null){
            user = new LoginUser();
            user.setUserId(1L);
        }
        gdfwAndBgqx.setUpdater(user.getUsername());
        gdfwAndBgqx.setUpdaterId(user.getUserId());
        gdfwAndBgqx.setUpdateTime(DateUtils.getNowDate());
        Integer i=gdfwAndBgqxMapper.editzd(gdfwAndBgqx);
        return 1;
    }

    @Override
    public List<YqdaGdfwAndBgqx> gettable(Long parentId,Long categoryId) {
        return gdfwAndBgqxMapper.gettable(parentId,categoryId);
    }

    @Override
    public int addtj(YqdaGdfwAndBgqxRule gdfwAndBgqxrule) {
        gdfwAndBgqxrule.setId(SnowflakeIdGenerator.generateId());
        Integer i=gdfwAndBgqxMapper.addtj(gdfwAndBgqxrule);
        return 1;
    }

    @Override
    public int edittj(YqdaGdfwAndBgqxRule gdfwAndBgqxrule) {
        Integer i=gdfwAndBgqxMapper.edittj(gdfwAndBgqxrule);
        return 1;
    }

    @Override
    public List<Map<String,Object>> gettjtable(Long zdId,Integer type) {
        return gdfwAndBgqxMapper.gettjtable(zdId,type);
    }

    @Override
    public String gettjsql(Long zdId,Integer type) {
        String sql=" and (";
        List<Map<String,Object>> list=gettjtable(zdId,type);
        for (int i = 0; i <list.size(); i++) {
            String fieldName=list.get(i).get("FIELD_NAME").toString();
            String likepos=list.get(i).get("LIKEPOS").toString();
            String relation=list.get(i).get("RELATION").toString();
            String [] aa=list.get(i).get("KEYWORD").toString().split("\\|");
            if(i==0){
                sql+="(";
            }else{
                sql+=relation;
                sql+="(";
            }
            for (int j = 0; j < aa.length; j++) {
                sql+=fieldName+" like '";
                if("左模糊".equals(likepos)||"模糊".equals(likepos)){
                    sql+="%";
                }
                sql+=aa[j];
                if("右模糊".equals(likepos)||"模糊".equals(likepos)){
                    sql+="%";
                }
                if(j<aa.length-1){
                    sql+=" or ";
                }
                sql+="'";
            }
            sql+=")";
        }
        sql+=")";
        return sql;
    }

    @Override
    public YqdaGdfwAndBgqx getzd(Long zdid) {
        return gdfwAndBgqxMapper.getzd(zdid);
    }

    @Override
    public int delete(Long tjid) {
        return gdfwAndBgqxMapper.delete(tjid);
    }

    private List<TreeSelect> getchildrens(Long id,Long categoryId) {
        YqdaGdfwAndBgqx yqTrees=new YqdaGdfwAndBgqx();
        List<YqdaGdfwAndBgqx> list = gdfwAndBgqxMapper.getchildrens(id,categoryId);
        List<TreeSelect> chilTrees = new ArrayList<>();
        for(YqdaGdfwAndBgqx yqdaTrees:list){
            TreeSelect chilTree = new TreeSelect();
            chilTree.setId(yqdaTrees.getId());
            chilTree.setLabel(yqdaTrees.getName());
            chilTree.setOrder_no(yqdaTrees.getOrderNo());
            chilTree.setBgqx(yqdaTrees.getBgqx());
            chilTree.setChildren(getchildrens(yqdaTrees.getId(),categoryId));
            chilTrees.add(chilTree);
        }
        return chilTrees;
    }

}
